"use strict"

function addRainbowkite()
{
    var html = '<div class="rainbowkite_html" id="rainbowkite_html" style="display: none;"> </div>'
    jQuery('body').append(html);

}

async function Head_ConnectWallet(e)
{
  if(e!=null&&e!=undefined)
  {
    $("#rainbowkite_html #"+e).click();
  }
  else{
    var address = $("#rainbowkite_html #address").html();
    if( address != "" && address !=null && address !=undefined && Helper.addressCur==null ){
        ConnectWallet(address)
    }
    else{
      $("#rainbowkite_html #openConnectModal").click();
      $("#rainbowkite_html #openChainModal").click();
    }
  }
}

async function ConnectWallet(address)
{
  if(address!=null){
      await Helper.sleep(100);
      await Helper.ConnectWallet(address);
      if( Helper.addressCur )
      {
          InitBalanceOf(Helper.addressCur);
          InitBlockChain(Helper.addressCur);
          Helper.SetInviter();

          // 默认打开充值页面，只打开一次
          if(Helper.openM&&sessionStorage.getItem(Helper.openM)!="true"){
            var array = Helper.openM.split("!");
            $('#'+array[0]).modal('show');
            sessionStorage.setItem(Helper.openM,"true")
          }
      }
  }
}

async function InitBalanceOf(address) 
{
  try{
    document.getElementById("head_chainicon").appendChild(document.getElementById("chainicon").cloneNode(true));

    $("#head_amount").html( $("#__next #displayBalance").html() );
    $("#head_navbar_center_btn").show();

    $("#copyright_address").html( Helper.Simplify3(address,true) );
    $("#copyright_address").parent().parent().show();
    $("#head_address").html( Helper.Simplify3(address,true) );
    $("#head_address").parent().removeClass("d-none");

    $(".My_Assets").removeClass("d-none");

    $("#Mask_group1").hide();
    $("#Mask_group2").show();
  }catch(e){
    $('#btn_head_ConnectWallet').show();
  }

}

async function InitBlockChain(address) 
{

}

function input_onKeyPress(event){
  var input = event.currentTarget
  var position = Helper.getSelectPosition(input)
  // 禁止在前面插入小数点
  if(event.keyCode==46&&position<input.value.length-4)
    return event.returnValue=false;

  // 允许在前面插入数字
  if( (!(event.keyCode<48 || event.keyCode>57)) && position-1<input.value.indexOf("."))
    return event.returnValue=true;

  if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d\d\d$/.test(input.value)){
    event.returnValue=false;
  }
}


function select_dropdownitem(e){

}

function DisabledBtn(e,time){
  $(e).addClass('blogcradbtnDis');
  if(time!=undefined){
    setTimeout("EnabledBtn('"+ e +"')", time);
  }
}

function EnabledBtn(e){
  $(e).removeClass('blogcradbtnDis');
}

var addressOld = null;
async function checkRainbowkitAddress(){
  var addressOld = null;
  while( true ){

    try{
      let network = await Helper.provider.getNetwork();      
    }catch(e){
      if(e.toString().indexOf("underlying network changed")!=-1){
        window.location.href = "javascript:location.reload();";
      }
    }

    var address_html = $("#rainbowkite_html #openAccountModal #address");
    if(addressOld!=address_html.html()){
      if(addressOld!=null){
        console.log(addressOld,address_html.html());
        window.location.href = "javascript:location.reload();";
      }
      else{
        addressOld = address_html.html();
        Helper.addressCur = null;
        Helper.sessionToken = "";
        Helper.inviter = "";

        ConnectWallet(addressOld);
      }
    }
    await Helper.sleep(1000);
  }
}

(function($) {
    "use strict"
	  addRainbowkite();

    Helper.CurServerIP = "http://"+Helper.getQueryVariable("ServerIP");
    Helper.inviter = Helper.getQueryVariable("inviter");
    Helper.openM    = Helper.getQueryVariable("openM");
    
    var href = window.location.href;
    $("#rainbowkite_html").load("../_next/server/pages/index.html", async function(){
          //window.location.href = href;

        while( $("#rainbowkite_html button").parent()[0] == null ){
          await Helper.sleep(100);
        }
        
        checkRainbowkitAddress();

        // $("#rainbowkite_html button").parent()[0].addEventListener('DOMNodeRemoved',async function(e){
        //     if(e.target.id=="openConnectModal") {
        //         console.log("成功连接钱包");
        //         await Helper.sleep(100);
        //         ConnectWallet($("#rainbowkite_html #address").html());
        //     }
        //     else
        //     if(e.target.id=="openAccountdiv") {
        //         console.log("断开连接钱包");
        //         window.location.href = "javascript:location.reload();";
        //     }
        // });

        if(Helper.openM&&Helper.addressCur==null){
              await Helper.sleep(2000);
              $("#rainbowkite_html #openConnectModal").click();
        }

    });



})(jQuery);


